package abe;



import java.util.ArrayList;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class SearchableTreeNode extends DefaultMutableTreeNode {
	private SearchableTreeNodeUserData userData = null;
	
	public SearchableTreeNode(SearchableTreeNodeUserData userData) {
		super(userData.getLabel());
		this.userData = userData;
	}

	public void  searchAndExpand(List <TreeNode[]> treePathList,String searchText){
		String label = getUserData().getLabel();
		
	
		for (int i = 0; i <getChildCount(); i++) {
			Object o = getChildAt(i);
			if (o instanceof SearchableTreeNode){
				SearchableTreeNode node = (SearchableTreeNode) o;
				node.searchAndExpand(treePathList,searchText);
			}else{
				System.err.println(o.getClass());
			}
		}

		
		if (label.contains(searchText)&&!searchText.trim().isEmpty()&&isLeaf()){
			//System.out.println("SearchableTreeNode.searchAndExpand(): match");
			treePathList.add(getPath());
		}		
		

	}
	public void  getAllPaths(List <TreeNode[]> treePathList){				
		treePathList.add(getPath());
		
		for (int i = 0; i <getChildCount(); i++) {
			Object o = getChildAt(i);
			if (o instanceof SearchableTreeNode){
				SearchableTreeNode node = (SearchableTreeNode) o;
				node.getAllPaths(treePathList);
			}else{
				System.err.println(o.getClass());
			}

		}
		

	}

	public SearchableTreeNodeUserData getUserData() {
		return userData;
	}

	public void setUserData(SearchableTreeNodeUserData userData) {
		this.userData = userData;
	}
	 
}
